﻿using Future.Common.Const;

namespace Future.Web.Controllers
{
    /// <summary>
    /// 菜单控制器
    /// </summary>
    public class MenuController : AuthorizeController
    {
        private readonly IMenuBLL _menuBLL;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="menuBLL"></param>
        public MenuController(IMenuBLL menuBLL)
        {
            _menuBLL = menuBLL;
        }

        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <param name="menuModel"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission("system:menu:add")]
        public bool CreateMenu(MenuModel menuModel)
        {
            if (menuModel.MenuType == MenuTypeEnum.Catalogue && !menuModel.Router.StartsWith("/"))
            {
                throw new Exception("目录路由必须以\"/\"开头");
            }

            bool result = _menuBLL.CreateMenu(menuModel);
            return result;
        }

        /// <summary>
        /// 获取当前登录用户的前端路由
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<VueRouterVo> GetRouterInfo()
        {
            Guid userId = CurrentUser.User.Id;
            bool isAdmin = CurrentUser.User.UserName == SystemConst.ADMIN;
            List<VueRouterVo> result = _menuBLL.GetAllMenus(userId, isAdmin);
            return result;
        }

        /// <summary>
        /// 条件搜索获取菜单列表
        /// </summary>
        /// <param name="menuQueryDto"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission("system:menu:query")]
        public List<MenuModel> GetMenuList([FromQuery] MenuQueryDto menuQueryDto)
        {
            List<MenuModel> result = _menuBLL.GetMenuList(menuQueryDto);
            return result;
        }

        /// <summary>
        /// 根据主键获取菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{id}")]
        public MenuModel GetMenuById([FromRoute] Guid id)
        {
            MenuModel menuModel = _menuBLL.GetMenuById(id);
            return menuModel;
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="menuModel"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission("system:menu:edit")]
        public bool Update(MenuModel menuModel)
        {
            if (menuModel.MenuType == MenuTypeEnum.Catalogue && !menuModel.Router.StartsWith("/"))
            {
                throw new Exception("目录路由必须以\"/\"开头");
            }

            bool result = _menuBLL.Update(menuModel);
            return result;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{id}")]
        [Permission("system:menu:remove")]
        public bool DeleteMenu([FromRoute] Guid id)
        {
            return _menuBLL.DeleteMenu(id);
        }

        /// <summary>
        /// 根据角色id获取该角色下全部菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{roleId}")]
        public List<MenuModel> GetListByRoleId(Guid roleId)
        {
            return _menuBLL.GetListByRoleId(roleId);
        }
    }
}
